#include "board.hpp"

Board::Board()
{
    srand(time(NULL));
    init_board();
}

void Board::init_board()
{
    Card* tab_card_random;
    tab_card_random=init_tab_random_free_card();
    unsigned int y(0);
    unsigned int cpt_tab_card_rand(0);

    plate[0][0].type_card=0;
    plate[0][6].type_card=1;
    plate[6][6].type_card=2;
    plate[6][0].type_card=3;

    plate[2][0].type_card=plate[2][2].type_card=plate[4][0].type_card=4;

    plate[0][2].type_card=plate[0][4].type_card=plate[2][4].type_card=5;

    plate[2][6].type_card=plate[4][4].type_card=plate[4][6].type_card=6;

    plate[4][2].type_card=plate[6][2].type_card=plate[6][4].type_card=7;

    for(int x=0 ; x < SIZE ; ++x)
    {
        if(x%2 == 0)
            y=1;
        else
            y=0;

        while(y < SIZE)
        {
            plate[x][y].set_all(tab_card_random[cpt_tab_card_rand]);
            ++cpt_tab_card_rand;

            if(x%2 == 0)
                y+=2;
            else
                ++y;
        }
    }

    free_card=tab_card_random[cpt_tab_card_rand];

    delete []tab_card_random;
}

void Board::push_east(const unsigned int & lign)
{
    Card tmp;

    for(unsigned int i(0) ; i < SIZE ; ++i)
    {
        tmp=plate[lign][i];
        plate[lign][i]=free_card;
        free_card=tmp;
    }
}

void Board::push_west(const unsigned int & lign)
{
    Card tmp;

    for(int i(SIZE-1) ; i >= 0 ; --i)
    {
        tmp=plate[lign][i];
        plate[lign][i]=free_card;
        free_card=tmp;
    }
}

void Board::push_north(const unsigned int & column)
{
    Card tmp;

    for(int i(SIZE-1) ; i >= 0 ; --i)
    {
        tmp=plate[i][column];
        plate[i][column]=free_card;
        free_card=tmp;
    }
}

void Board::push_south(const unsigned int & column)
{
    Card tmp;

    for(unsigned int i(0) ; i < SIZE ; ++i)
    {
        tmp=plate[i][column];
        plate[i][column]=free_card;
        free_card=tmp;
    }
}

bool Board::is_in_same_position(const Treasure & treasure)const
{
    for(auto & lec:t)
    {
        if(lec.position_x == treasure.position_x && lec.position_y == treasure.position_y)
            return true;
    }

    return false;
}

/*
bool Board::can_go_to(const unsigned int & x1,const unsigned int & y1,const unsigned int & x2,const unsigned int & y2)
{

}
*/

Card* Board::init_tab_random_free_card()
{
    Card* r_random_card=new Card[MOVING_CARD];
    unsigned int choice_card(0);
    int nb_card[3]= {0,0,0};
    bool go_out(false);

    for(unsigned int cpt(0) ; cpt < MOVING_CARD ; ++cpt)
    {
        choice_card=random()%3;

        while(!go_out)
        {
            switch(choice_card)
            {
            case 0:
                if(nb_card[choice_card] < 16)
                {
                    nb_card[choice_card]++;
                    go_out=true;
                }
                else
                    choice_card++;
                break;
            case 1:
                if(nb_card[choice_card] < 6)
                {
                    nb_card[choice_card]++;
                    go_out=true;
                }
                else
                    choice_card++;
                break;
            case 2:
                if(nb_card[choice_card] < 12)
                {
                    nb_card[choice_card]++;
                    go_out=true;
                }
                else
                    choice_card=0;
                break;
            }
        }

        go_out=false;

        if(choice_card == 0)
            r_random_card[cpt].set_random_card_t0();
        else if(choice_card == 1)
            r_random_card[cpt].set_random_card_t1();
        else
            r_random_card[cpt].set_random_card_t2();
    }

    return r_random_card;
}